package cn.iocoder.yudao.module.erp.service.finance.financein;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.financein.vo.FinanceInSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.financein.FinanceInDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.saleorder.SaleOrderDO;
import cn.iocoder.yudao.module.erp.dal.mysql.finance.financein.FinanceInMapper;
import cn.iocoder.yudao.module.erp.dal.mysql.saleorder.SaleOrderMapper;
import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.FINANCE_IN_NOT_EXISTS;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.FINANCE_IN_NO_EXISTS;

/**
 * 收入单 Service 实现类
 *
 * @author huey
 */
@Service
@Validated
@Slf4j
public class FinanceInServiceImpl implements FinanceInService {

    @Resource
    private FinanceInMapper financeInMapper;
    @Resource
    private ErpNoRedisDAO noRedisDAO;
    @Resource
    private SaleOrderMapper saleOrderMapper;

    @Override
    public Long createFinanceIn(FinanceInSaveReqVO createReqVO) {
        log.info("createFinanceIn: {}", createReqVO);
        // 插入
        FinanceInDO financeIn = BeanUtils.toBean(createReqVO, FinanceInDO.class);
        // 校验订单号
        if (StrUtil.isEmpty(financeIn.getBillNo())) {
            financeIn.setBillNo(noRedisDAO.generate(ErpNoRedisDAO.FINANCE_RECEIPT_NO_PREFIX));
        }else {
            FinanceInDO financeInDO = financeInMapper.selectByNo(financeIn.getBillNo());
            if (ObjectUtil.isNotEmpty(financeInDO)) {
                throw exception(FINANCE_IN_NO_EXISTS);
            }
        }
        financeInMapper.insert(financeIn);
        // 更新销售单状态
        saleOrderMapper.updateById(new SaleOrderDO().setId(financeIn.getLinkId()).setSyncPay(true));
        // 返回
        return financeIn.getId();
    }

    @Override
    public void updateFinanceIn(FinanceInSaveReqVO updateReqVO) {
        // 校验存在
        validateFinanceInExists(updateReqVO.getId());
        // 更新
        FinanceInDO updateObj = BeanUtils.toBean(updateReqVO, FinanceInDO.class);
        financeInMapper.updateById(updateObj);
    }

    @Override
    public void deleteFinanceIn(Long id) {
        // 校验存在
        validateFinanceInExists(id);
        // 删除
        financeInMapper.deleteById(id);
    }

    private void validateFinanceInExists(Long id) {
        if (financeInMapper.selectById(id) == null) {
            throw exception(FINANCE_IN_NOT_EXISTS);
        }
    }

    @Override
    public FinanceInRespVO getFinanceIn(Long id) {
        FinanceInDO financeInDO = financeInMapper.selectById(id);
        if (ObjectUtil.isEmpty(financeInDO)) {
            throw exception(FINANCE_IN_NOT_EXISTS);
        }
        SaleOrderDO saleOrderDO = saleOrderMapper.selectById(financeInDO.getLinkId());
        FinanceInRespVO bean = BeanUtils.toBean(financeInDO, FinanceInRespVO.class);
        bean.setItem(BeanUtils.toBean(saleOrderDO, FinanceInRespVO.Item.class));
        return bean;
    }

    @Override
    public PageResult<FinanceInDO> getFinanceInPage(FinanceInPageReqVO pageReqVO) {
        return financeInMapper.selectPage(pageReqVO);
    }

}